package com.fpx.order.plat.bussiness.plat.smt.task;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fpx.order.plat.bussiness.dao.log.IPullOrderLogDao;
import com.fpx.order.plat.bussiness.entity.CustomerShop;
import com.fpx.order.plat.bussiness.entity.PullOrderLog;
import com.fpx.order.plat.bussiness.entity.PullorderLogFail;
import com.fpx.order.plat.bussiness.plat.base.PlatOrderCurrentJob;
import com.fpx.order.plat.bussiness.plat.smt.service.SmtPlatOrderHandler;
import com.fpx.order.plat.bussiness.system.PlatConstants;
import com.fpx.order.plat.bussiness.vo.OrderDealResult;


/**
 * desc:
 * @author xiangtao(xiangtao@4px.com)
 * @date 2015年11月24日 下午4:38:04
 * @version 1.0
 */
@Component
public class SmtReceiveOrderJob extends PlatOrderCurrentJob {
	
	private final static Logger LOGGER = Logger.getLogger(SmtReceiveOrderJob.class);
	
	@Autowired
	private SmtPlatOrderHandler smtPlatOrderHandler;
	
	
	@Autowired
	private IPullOrderLogDao pullOrderLogDao;
	
	/**
	 * 定时任务
	 */
	public void exec(){
		LOGGER.info("【速卖通 SMT】 自动拉单开始.....");
		
		 DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 System.out.println(df.format(new Date()));
		
		initMeodth(PlatConstants.ORDER_SOURCE_SMT);
		List<CustomerShop> customerLst=super.getCustomers();
		PullOrderLog pullOrderLog=new PullOrderLog();
		if(!org.springframework.util.StringUtils.isEmpty(customerLst)){
		// 遍历商家和店铺
		 for (CustomerShop customer : customerLst) {
			 List<OrderDealResult> orderDealResultLst=null;
			 String cm_id=customer.getCm_id();
			 String cs_id=customer.getCs_id();
		    try{
		    	pullOrderLog.setFk_cm_id(customer.getCm_id());//客户id
				pullOrderLog.setFk_cs_code(customer.getCs_id());//商铺ID
				pullOrderLog.setCs_platform_code(PlatConstants.ORDER_SOURCE_SMT);//平台号
				Date beginTime = new Date(); 
				pullOrderLog.setPull_date_begin(dateFormat.format(beginTime));//拉单开始时间
//				if(cm_id.equals("550277")&&cs_id.equals("481")){
					orderDealResultLst=smtPlatOrderHandler.getOrderByCustomer(customer, null, null, 0);
					if(orderDealResultLst==null||orderDealResultLst.size()<=0){
						throw new RuntimeException("【SMT】---商家：[" + cm_id + "]-商铺ID:["+cs_id+"],没有获取到订单");
			    	}
//		    	}
		     }catch (Exception ex) {
		    	   LOGGER.error("【SMT】---商家:["+cm_id+"]-商铺ID:["+cs_id+"]-【异常信息】: "+ex.getMessage());
		           pullOrderLog.setPull_sign(PlatConstants.IS_PULL_SIGN_N);//拉单失败
				   Date endTime = new Date(); 
				   pullOrderLog.setPull_date_end(dateFormat.format(endTime));//拉单结束时间
			    try{
				   PullorderLogFail LogFail=new PullorderLogFail();
				   LogFail.setError_msg(ex.getMessage());
				   pullOrderLogDao.savePullOrderLog(pullOrderLog);
				   LogFail.setFk_pol_id(pullOrderLog.getPol_id());
				   pullOrderLogDao.savePullLogFail(LogFail);
				  }catch(Exception e){
				    LOGGER.error("【SMT】---商家:["+cm_id+"]-商铺ID:["+cs_id+"]-【保存SMT拉单失败日志时异常】  "+e.getMessage());
				  }	
		   }
		    if(orderDealResultLst !=null&& orderDealResultLst.size()>0){
		    	Date endTime = new Date(); 
				pullOrderLog.setPull_date_end(dateFormat.format(endTime));//拉单结束时间
				pullOrderLog.setPull_sign(PlatConstants.IS_PULL_SIGN_Y);
				try{
				   pullOrderLogDao.savePullOrderLog(pullOrderLog);
				}catch(Exception e){
				   LOGGER.error("【SMT】---商家:["+cm_id+"]-商铺ID:["+cs_id+"]-【保存SMT拉单成功日志时异常】  "+e.getMessage());
				}    
		    }
		 } 
	 }
      LOGGER.info("【速卖通 SMT】 自动拉单结束");
   }
	
}
